#region License
// Copyright 2008-2009 Jeremy Skinner (http://www.jeremyskinner.co.uk)
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// The latest version of this file can be found at http://www.codeplex.com/FluentValidation
#endregion

namespace FluentValidation.Internal {
    using System.Collections.Generic;
    using System.Linq;
    using Results;

    public class ComplexPropertyRule<T, TProperty> : IPropertyRule<T> {
        readonly PropertyModel<T, TProperty> model;
        readonly IEnumerable<IValidationRule<TProperty>> validator;

        public ComplexPropertyRule(IEnumerable<IValidationRule<TProperty>> validator, PropertyModel<T, TProperty> model) {
            this.validator = validator;
            this.model = model;
        }

        #region IPropertyRule<T> Members
        public string PropertyName {
            get { return model.Identifier; }
            set { model.Identifier = value; }
        }

        public string CustomPropertyName {
            get { return model.CustomPropertyName; }
            set { model.CustomPropertyName = value; }
        }

        public string PropertyDescription {
            get { return model.PropertyDescription; }
        }

        public string Member {
            get { return model.Identifier; }
        }

        public IEnumerable<ValidationFailure> Validate(ValidationContext<T> context) {
            //if (Member == null) {
            //    throw new InvalidOperationException(string.Format("Nested validators can only be used with Member Expressions. '{0}' is not a MemberExpression.", model));
            //}

            string propertyName = BuildPropertyName(context);

            if (! context.Selector.CanExecute(this, propertyName)) {
                return Enumerable.Empty<ValidationFailure>();
            }

            TProperty instanceToValidate = model.PropertyFunc(context.InstanceToValidate);

            if (instanceToValidate == null) {
                return Enumerable.Empty<ValidationFailure>();
            }

            var propertyChain = new PropertyChain(context.PropertyChain);
            propertyChain.Add(Member);

            //Selector should not propogate to complex properties. 
            //If this property has been included then all child properties should be included.

            var newContext = new ValidationContext<TProperty>(instanceToValidate, propertyChain, new DefaultValidatorSelector());
            IEnumerable<ValidationFailure> results = validator.SelectMany(x => x.Validate(newContext));

            return results;
        }
        #endregion

        string BuildPropertyName(ValidationContext<T> context) {
            var chain = new PropertyChain(context.PropertyChain);
            chain.Add(model.Identifier);
            return chain.ToString();
        }
    }
}